package com.dianshang.controller.auth;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dianshang.pojo.system.Admin;
import com.dianshang.service.system.AdminService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

public class AdminController implements UserDetailsService {

    @Reference
    private AdminService adminService;

    /**
     * 查询管理员
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
        Admin admin = adminService.findByName(username);
        if(admin == null){
            return null;
        }
        // 构建角色集合 ，项目中此处应该是根据用户名查询用户的角色列表
        List<GrantedAuthority> grantedAuths= new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        return new User(username,admin.getPassword(),grantedAuths);
    }

}